<template>
  <el-dialog title="选择通知人员" :visible.sync="show" width="887px" append-to-body custom-class="video_dialog">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="人员名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入预案名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryId">
        <treeselect
          v-model="queryParams.directoryId"
          size="small"
          :options="treeCommunicateData"
          :normalizer="normalizer"
          placeholder="选择目录"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="personLoading"
      :data="personList"
      style="width:100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="目录名称" align="center" prop="directoryName" />
      <el-table-column label="人员名称" align="center" prop="userName" />
      <el-table-column label="人员职务" align="center" prop="userDuty" />
      <el-table-column label="电话" align="center" prop="phoneNum" />
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listDirectory } from '@/api/fjgs/warning/contacts.js'
import { listPerson } from '@/api/fjgs/warning/contacts.js'
import { handleTree } from '@/utils/ruoyi'
import { cloneDeep } from 'loadsh'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: {
    Treeselect
  },
  data() {
    return {
      show: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        directoryId: null,
        userName: null
      },
      personLoading: false,
      personList: [],
      communicateData: [],
      ids: [],
      selection: []
    }
  },
  computed: {
    treeCommunicateData() {
      return handleTree(this.communicateData)
    }
  },
  created() {
    this.getDirectoryList()
  },
  methods: {
    open() {
      this.show = true
      this.handleQuery()
    },
    cancel() {
      this.ids.length = 0
      this.show = false
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    getList() {
      this.personLoading = true
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows
        this.total = response.total
        this.personLoading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    submitForm() {
      this.$emit('success', cloneDeep(this.selection) || [])
      this.show = false
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.directoryName,
        children: node.children
      }
    },
    getDirectoryList() {
      this.directoryLoading = true
      listDirectory(this.queryParams).then(response => {
        this.communicateData = response.rows
        // this.total = response.total
        this.directoryLoading = false
      })
    }
  }
}
</script>

<style>
.video_dialog .el-dialog__body{
    padding: 4px;
}
</style>
